

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.security.auth.login.LoginException;


public class LoginClass implements Login{
	
	private ArrayList<Course> arrayCourse;
	private ArrayList<SessionClass> room;
	
	public LoginClass() throws RemoteException {
		arrayCourse = new ArrayList<Course>();
		room = new ArrayList<>();
	}
	
	public void addNewCourse(Course newCourse){
		this.arrayCourse.add(newCourse);
	}
	
	public void removeCourse(Course course){
		this.arrayCourse.remove(course);
	}
	
	public ArrayList<Course> getCourse(){
		return arrayCourse;
	}

	@Override
	public Session login(String username, char[] password)
			throws RemoteException, LoginException {
		Student temp = checkLogin(username, password);
		if(temp!= null){
			SessionClass a = new SessionClass(temp);
			room.add(a);
			return room.get(room.indexOf((SessionClass)a));
		}else{
			return null;
		}
	}
	
	private Student checkLogin(String username, char[] pass){
		for(Course e : arrayCourse){
			for(Student stu : e.getStudentList()){
				if(stu.getUserName().equals(username)) {
					if(stu.getPass().equals(String.valueOf(pass))){
						return stu;
					}
				}
			}
		}
		return null;
	}

}
